package net.jlibrary.account.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import net.jlibrary.account.action.vo.EmployeeAdminActionVo;
import net.jlibrary.account.service.IEmployeeManager;
import net.jlibrary.common.CoreConstants;
import net.jlibrary.common.core.annotation.Token;
import net.jlibrary.common.core.entity.Pagination;
import net.jlibrary.common.core.entity.PaginationParam;
import net.jlibrary.common.core.service.ISessionUser;
import net.jlibrary.common.project.domain.Group;
import net.jlibrary.common.project.domain.PermissionRole;
import net.jlibrary.group.domain.Area;
import net.jlibrary.group.service.IMerchantManager;

@RestController
@RequestMapping("/account")
public class EmployeeAdminAction {
	@Resource
	private IEmployeeManager employeeManager;
	@Resource
	private IMerchantManager merchantManager;
	@Resource
	private ISessionUser sessionUser;

	@RequestMapping(value = "/employeeAdminView", method = { RequestMethod.GET })
	public ModelAndView view() {
		return new ModelAndView("/account/employeeAdminView");
	}

	@RequestMapping(value = "/employeeAdminJsonList", method = { RequestMethod.POST })
	public Pagination lists(EmployeeAdminActionVo employee, PaginationParam param) {
		/*
		 * if (employee.getGroup() == null || employee.getGroup().getId() == null) {
		 * employee.setCode("===");// 未指定机构不显示 }
		 */
		Pagination pagination = employeeManager.listsPage(employee, param);
		return pagination;
	}

	@RequestMapping(value = "/employeeAdminJsonDetail", method = { RequestMethod.GET })
	public EmployeeAdminActionVo detail(EmployeeAdminActionVo employee) {
		employee = employeeManager.get(employee);
		List<PermissionRole> list = employeeManager.roles();
		for (int i = 0; i < list.size(); i++) {
			if (employee.getRoles() != null) {
				if (list.get(i).getId() == employee.getRoles()[0]) {
					employee.setRole(list.get(i).getName());
				}
			}
		}
		return employee;
	}

	@RequestMapping(value = "/employeeAdminJsonAddFormInit", method = { RequestMethod.GET })
	public EmployeeAdminActionVo addFormInit() {
		EmployeeAdminActionVo employee = new EmployeeAdminActionVo();
		return employee;
	}

	@RequestMapping(value = "/employeeAdminJsonSave", method = { RequestMethod.POST })
	@Token
	public ModelMap save(@ModelAttribute("vm") EmployeeAdminActionVo employee, @Token String token,
			BindingResult result) {
		employeeManager.save(employee);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/employeeAdminJsonUpdate", method = { RequestMethod.POST })
	@Token
	public ModelMap update(@ModelAttribute("vm") EmployeeAdminActionVo employee, @Token String token,
			BindingResult result) {
		employeeManager.update(employee);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/employeeAdminJsonDelete", method = { RequestMethod.POST })
	public ModelMap delete(Long id) {
		EmployeeAdminActionVo employee = new EmployeeAdminActionVo();
		employee.setId(id);
		employeeManager.delete(employee);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/employeeAdminJsonRenew", method = { RequestMethod.POST })
	public ModelMap renew(Long id) {
		EmployeeAdminActionVo employee = new EmployeeAdminActionVo();
		employee.setId(id);
		employeeManager.renew(employee);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/groupAdminJsonData", method = { RequestMethod.GET })
	public List<ModelMap> groupAdminJsonData() {
		List<Group> ls = merchantManager.listsGroup();
		List<ModelMap> maps = new ArrayList<ModelMap>();
		for (Group g : ls) {
			ModelMap map = new ModelMap();
			map.put(CoreConstants.VIEW_SELECT_LABEL_VALUE, g.getPrimaryKey());
			map.put(CoreConstants.VIEW_SELECT_LABEL_TEXT, g.getShortName());
			maps.add(map);
		}
		return maps;
	}

	@RequestMapping(value = "/areaAdminJsonData", method = { RequestMethod.GET })
	public List<ModelMap> areaAdminJsonData() {
		List<Area> areas = merchantManager.listAreas();
		List<ModelMap> maps = new ArrayList<ModelMap>();
		for (Area area : areas) {
			ModelMap map = new ModelMap();
			map.put(CoreConstants.VIEW_SELECT_LABEL_VALUE, area.getPrimaryKey());
			map.put(CoreConstants.VIEW_SELECT_LABEL_TEXT, area.getName());
			maps.add(map);
		}
		return maps;
	}

	@RequestMapping(value = "/permissionRolesAdminJsonData", method = { RequestMethod.GET })
	public List<ModelMap> permissionRolesAdminJsonData() {
		List<PermissionRole> list = employeeManager.roles();
		List<ModelMap> maps = new ArrayList<ModelMap>();
		for (PermissionRole roleVo : list) {
			ModelMap map = new ModelMap();
			map.put(CoreConstants.VIEW_SELECT_LABEL_VALUE, roleVo.getPrimaryKey());
			map.put(CoreConstants.VIEW_SELECT_LABEL_TEXT, roleVo.getName());
			maps.add(map);
		}
		return maps;
	}

}
